package com.xc.freeapp.account.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Vaccineperson;
import com.xc.freeapp.entity.Vaccines;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.VaccinepersonService;
import com.xc.freeapp.service.VaccinesService;
import com.xc.freeapp.util.TokenAnnotation;


@Controller
@RequestMapping("/v2/vassines")
@Api(value="v2vassines",description = "接种人疫苗")
public class VaccinesControllerV2 extends AbstractController {
	@Autowired
	private VaccinesService vaccinesServiceImpl;
	@Autowired
	private VaccinepersonService vaccinepersonServiceImpl;

	/**
	 * 获取接种人疫苗列表
	 * 
	 * @param requestMap
	 * @return
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取接种人疫苗列表")
	public List getVaccinesListByVaccinePerson(
			@RequestParam("vaccineperid") String vaccineperid)
			throws BaseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		List list=new ArrayList();
		Map map2 =new LinkedHashMap();
		Map map =new LinkedHashMap();
		Vaccineperson vaccineperson = vaccinepersonServiceImpl.selectByPrimaryKey(Integer.valueOf(vaccineperid));
		List<Vaccines> vaccinesList = vaccinesServiceImpl
				.getVaccinesList(Integer.valueOf(vaccineperid));
		List<Vaccines> vaccineslists=new ArrayList<Vaccines>();
		for (Vaccines vaccines : vaccinesList) {
			if(vaccineperson!=null&&vaccineperson.getBirthday()!=null){
				cal.setTime(vaccineperson.getBirthday());
				cal.add(Calendar.DAY_OF_YEAR, Integer.valueOf(vaccines.getVaccinatetime()));
				vaccines.setVaccinatetime(sdf.format(cal.getTime()));
			}
			
			if(map.containsKey(vaccines.getVaccinateAge())){
				vaccineslists.add(vaccines);
			}else{
				vaccineslists=new ArrayList<Vaccines>();
				vaccineslists.add(vaccines);
				map.put(vaccines.getVaccinateAge(), vaccineslists);
			}
		}
		
		for (Object key : map.keySet()) {
			  map2.put("key",key );
			  map2.put("value", map.get(key));
			  list.add(map2);
			  map2=new LinkedHashMap();
		}
		return list;

	}

	/**
	 * 更新接种人疫苗列表
	 * 
	 * @param requestMap
	 * @return
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "更新接种人疫苗列表")
	public ResponseBean updateVaccinesList(
			@RequestParam("vaccineperid") String vaccineperid,
			@RequestParam("vaccineType") String vaccineType,
			@RequestParam("ifdo") String ifdo)
			throws BaseException {

		vaccinesServiceImpl.updateifdo(Integer.valueOf(vaccineperid),
				Integer.valueOf(vaccineType), Integer.valueOf(ifdo));
		return ResponseBean.getSuccess();

	}

}
